﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using com.google.zxing;
using Microsoft.Phone.Controls;
using WP7_Barcode_Library;
using System.Windows.Media;
using MayhemWP7.Modules.Reactions;
using MayhemCore;

namespace MayhemWP7.Modules.Pages
{
    public partial class MayhemEventConfig : PhoneApplicationPage
    {
        private MayhemEvent module;
        public MayhemEventConfig()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Get the index of the connection in the connection list
            int connectionIndex = Int32.Parse(NavigationContext.QueryString["id"]);

            module = (MayhemEvent)Mayhem.Instance.ConnectionList[connectionIndex].Reaction;
            this.DataContext = module;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WP7BarcodeManager.ScanMode = BarcodeFormat.QR_CODE; //Tell barcode library what type of scan we want to perform

            new Timer(new TimerCallback(o => Dispatcher.BeginInvoke(new Action(() =>
            {
                
                textBlockError.Text = "Processing...";
                textBlockError.Foreground = new SolidColorBrush(Colors.White);
                
            }))), null, 500, Timeout.Infinite);

            WP7BarcodeManager.ScanBarcode(Barcode_Results);
        }

        /// <summary>
        /// Callback method that processes results returned by the WP7BarcodeManager. Results are also stored at WP7BarcodeManager.LastCaptureResults.
        /// </summary>
        /// <param name="BCResults">Object that holds all the results of processing the barcode. Results are also stored at WP7BarcodeManager.LastCaptureResults.</param>
        public void Barcode_Results(WP7_Barcode_Library.BarcodeCaptureResult Results)
        {
            if (Results.State == WP7_Barcode_Library.CaptureState.Success)
            {
                //     textBox1.Text = Results.BarcodeText; //Use results
                string[] results = Results.BarcodeText.Split(':');
                try
                {
                    module.Port = results[0];
                    module.IP = results[1];
                    module.Guid = results[2];
                    
                    NavigationService.GoBack();
                }
                catch
                {
                    textBlockError.Text = "Error scanning. Try again.";
                    textBlockError.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            else //Error occured
            {
                textBlockError.Text = "Error scanning. Try again.";
                textBlockError.Foreground = new SolidColorBrush(Colors.Red);

                if (Results.BarcodeImage != null)
                {
                    image1.Source = Results.BarcodeImage; //Display image
                }
                else
                {
                    //No image captured
                }
            }
        }
    }
}